<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增用户</title>
    <link rel="stylesheet" href="../css/list.css">
</head>

<body>

    <div>
        <div>
            <h1>新增用户</h1>
            <table>
                <tr>
                    <td>
                        姓名：<input name="name" type="text" placeholder="请输入姓名">
                    </td>
                    <td>
                        年龄：<input name="age" type="text" placeholder="请输入年龄">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="保存" onclick="saveUser()">
                        <input type="button" value="取消" onclick="cancelUser()">
                    </td>
                </tr>
            </table>
        </div>
    </div>


    <script src="../js/axios.min.js"></script>
    <script>
        function saveUser() {
            //获取表单的姓名
            let uname = document.querySelector('[name=name]').value;
            let uage = document.querySelector('[name=age]').value;
            let user = {
                name: uname,
                age: uage
            }
            console.log(user);

            //判断是否需要提交
            if (confirm("确认提交吗")) {
                axios.post('/user', user).then(() => {
                    location.href = '/users'  //重定向回列表页
                })
            }
        }

        function cancelUser() {
            console.log(222);
            // location.href = '/'
        }
    </script>
</body>

</html>